package com.ysd.lis.service.sta.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.sta.StaFeeitemVsItemdict;
import com.ysd.lis.entity.sta.StaItemDict;
import com.ysd.lis.mapper.sta.StaFeeitemVsItemdictMapper;
import com.ysd.lis.mapper.sta.StaItemDictMapper;
import com.ysd.lis.service.sta.StaFeeitemVsItemdictService;
import com.ysd.util.ToolsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 申请项目与报告项目对照表 服务实现类
 * </p>
 *
 * @author wh
 * @since 2025-01-17
 */
@Service
public class StaFeeitemVsItemdictServiceImpl extends ServiceImpl<StaFeeitemVsItemdictMapper, StaFeeitemVsItemdict> implements StaFeeitemVsItemdictService {

    @Autowired
    private StaItemDictMapper itemDictMapper;
    @Override
    public Result findItemDictListByFeeCode(String feeCode) {
        if (ToolsUtils.isEmpty(feeCode)) {
            return Result.fail("申请项目id入参异常");
        }
        MPJLambdaWrapper<StaItemDict> mpjLambdaWrapper = new MPJLambdaWrapper<>();
        mpjLambdaWrapper.selectAll(StaItemDict.class)
                .selectAs(StaFeeitemVsItemdict::getFeeCode, StaItemDict::getFeeCode)
                .selectAs(StaFeeitemVsItemdict::getId, StaItemDict::getFeeVSItemDictId)
                .selectAs(StaFeeitemVsItemdict::getItemDictId, StaItemDict::getItemDictId)
                .leftJoin(StaFeeitemVsItemdict.class, p->p.eq(StaFeeitemVsItemdict::getItemNo,StaItemDict::getItemNo).eq(StaItemDict::getDelFlag,0))
                .eq(StaFeeitemVsItemdict::getFeeCode, feeCode)
                .eq(StaFeeitemVsItemdict::getDelFlag, 0)
                .orderByAsc(StaFeeitemVsItemdict::getSeq);
        List<StaItemDict> itemDicts = itemDictMapper.selectList(mpjLambdaWrapper);
        return Result.succ(itemDicts);
    }

    @Override
    public Result batchAddFeeVsDictList(List<StaFeeitemVsItemdict> list) {
        boolean b = this.saveBatch(list);
        if (b) {
            return Result.succ(1, "添加成功", null);
        } else {
            return Result.fail("添加失败");
        }
    }

    @Override
    public Result batchDeleteFeeVsDictList(List<StaFeeitemVsItemdict> list) {
        List<String> ids= list.stream().map(StaFeeitemVsItemdict::getFeeVSItemDictId).filter(StringUtils::isNotBlank).collect(Collectors.toList());
        boolean b = this.removeByIds(ids);
        if (b) {
            return Result.succ(1, "删除成功", null);
        } else {
            return Result.fail("删除失败");
        }
    }
}
